package com.work.music.service;

import com.work.music.common.HttpRequestParamter;
import com.work.music.common.ResponseJson;
import com.work.music.dao.AdminLogMapper;
import com.work.music.dao.AdminMapper;
import com.work.music.entity.Admin;
import com.work.music.entity.AdminLog;
import com.work.music.exception.AjaxOperationFailException;
import com.work.music.utils.ContextUtil;
import com.work.music.utils.HttpUtil;
import com.work.music.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class AdminService extends BaseService {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private AdminLogMapper adminLogMapper;

    public ResponseJson login() throws AjaxOperationFailException {
        String username = HttpRequestParamter.getString("username");
        String password = HttpRequestParamter.getString("password");
        if(StringUtil.isEmpty(username))
            throw new AjaxOperationFailException("请输入用户名");
        if(StringUtil.isEmpty(password))
            throw new AjaxOperationFailException("请输入密码");
        Admin admin = adminMapper.selectByName(username);
        if(admin == null)
            throw new AjaxOperationFailException("该用户不存在");
        if(!StringUtil.md5(password).equals(admin.getAdminPwd()))
            throw new AjaxOperationFailException("密码错误");
        AdminLog adminLog = new AdminLog();
        adminLog.setAdminId(admin.getId());
        adminLog.setType(0);
        adminLog.setAddtime(new Date());
        adminLog.setAddip(HttpUtil.getClientIp());
        int tag = adminLogMapper.insert(adminLog);
        ContextUtil.setAttribute("admin", admin);
        return new ResponseJson();
    }

    public ResponseJson changePwd() throws AjaxOperationFailException {
        String oldPwd = HttpRequestParamter.getString("oldPwd");
        String newPwd = HttpRequestParamter.getString("newPwd");
        if(StringUtil.isNull(oldPwd) || StringUtil.isNull(newPwd))
            throw new AjaxOperationFailException("请完善资料");
        Admin admin = (Admin) ContextUtil.getAttribute("admin");
        if(!StringUtil.md5(oldPwd).equals(admin.getAdminPwd()))
            throw new AjaxOperationFailException("原密码错误");
        admin.setAdminPwd(StringUtil.md5(newPwd));
        int tag = adminMapper.updateByPrimaryKey(admin);
        if(tag <= 0)
            throw new AjaxOperationFailException("修改失败");
        return new ResponseJson();
    }

    public int loginOut() {
        Admin admin = (Admin) ContextUtil.getAttribute("admin");
        AdminLog adminLog = new AdminLog();
        adminLog.setAdminId(admin.getId());
        adminLog.setType(1);
        adminLog.setAddtime(new Date());
        adminLog.setAddip(HttpUtil.getClientIp());
        int tag = adminLogMapper.insert(adminLog);
        ContextUtil.setAttribute("admin", null);
        return tag;
    }

    /**
     * 获取管理员列表
     * @return
     */
    public ResponseJson adminList() {
        String name = HttpRequestParamter.getString("name");
        int page = HttpRequestParamter.getInt("page", 1);
        int offset = HttpRequestParamter.getInt("limit", 20);
        StringBuffer stringBuffer = new StringBuffer("where 1=1 ");
        if(!StringUtil.isNull(name)) {
            stringBuffer.append(" and admin_name like '%").append(name).append("%'");
        }
        ResponseJson responseJson = new ResponseJson();
        responseJson.setNum(adminMapper.selectCount(stringBuffer.toString()));
        List<Map<String, Object>> list = adminMapper.selectList(stringBuffer.toString(), (page - 1) * offset, offset);
        responseJson.setList(list);
        return responseJson;
    }


    public Admin selectAdminInfoById() {
        long id = HttpRequestParamter.getLong("id");
        Admin admin = adminMapper.selectByPrimaryKey(id);
        return admin == null ? new Admin() : admin;
    }

    /**
     * 修改管理员信息
     * @return
     */
    public ResponseJson editJson() throws AjaxOperationFailException {
        long id = HttpRequestParamter.getLong("id");
        String name = HttpRequestParamter.getString("name");
        String password = HttpRequestParamter.getString("password");
        Admin admin = adminMapper.selectByName(name);
        int tag;
        if(admin == null) {
            admin = new Admin();
            admin.setAdminName(name);
            admin.setAdminPwd(StringUtil.md5(password));
            admin.setAddtime(new Date());
            admin.setType(1);
            tag = adminMapper.insert(admin);
        } else {
            if(admin.getId() != id) {
                throw new AjaxOperationFailException("该账号已存在");
            }
            admin.setAdminPwd(StringUtil.md5(password));
            tag = adminMapper.updateByPrimaryKey(admin);
        }
        if(tag <= 0)
            throw new AjaxOperationFailException("操作失败");
        return new ResponseJson();
    }

    public ResponseJson delJson() throws AjaxOperationFailException {
        long id = HttpRequestParamter.getLong("id");
        Admin admin = adminMapper.selectByPrimaryKey(id);
        if(admin != null && admin.getType() == 1) {
            int tag = adminMapper.deleteByPrimaryKey(id);
            if(tag <= 0)
                throw new AjaxOperationFailException("删除失败");
        } else {
            throw new AjaxOperationFailException("删除失败：该账号为超级管理员，不能删除");
        }
        return new ResponseJson();
    }

    /**
     * 获取操作日志信息
     * @return
     */
    public ResponseJson adminLogList() {
        String name = HttpRequestParamter.getString("name");
        int page = HttpRequestParamter.getInt("page", 1);
        int offset = HttpRequestParamter.getInt("limit", 20);
        StringBuffer stringBuffer = new StringBuffer("where 1=1 ");
        if(!StringUtil.isNull(name)) {
            stringBuffer.append(" and a.admin_name like '%").append(name).append("%'");
        }
        ResponseJson responseJson = new ResponseJson();
        responseJson.setNum(adminLogMapper.selectLogCount(stringBuffer.toString()));
        List<Map<String, Object>> list = adminLogMapper.selectLogList(stringBuffer.toString(), (page - 1) * offset, offset);
        responseJson.setList(list);
        return responseJson;
    }
}
